import { WsClient } from "tsrpc-browser";
import { tgxSceneUtil, tgxUIAlert } from "../../core_tgx/tgx";
import { SubWorldSceneParams, SceneDef } from "../../scripts/SceneDef";
import { ServiceType } from "../shared/protocols/serviceProto_worldServer";
import { ResJoinSubWorld } from "../shared/protocols/worldServer/PtlJoinSubWorld";
import { SubWorldConfigItem, SubWorldConfig } from "../shared/SubWorldConfig";
import { UserInfo } from "../shared/types/UserInfo";
import { NetUtil } from "./NetUtil";

let _worldConn: WsClient<ServiceType> = null;
let _worldServerUrl = '';
let _playerNum: number = 0;
let _subWorldId: string;
let _subWorldConfig: SubWorldConfigItem;
let _subWorldDisplayName: string;

let _params: SubWorldSceneParams;
let _currentUser: UserInfo;

export class WorldMgr {
    public static async doEnterWorld(params: SubWorldSceneParams) {
        _params = params;
        _worldServerUrl = params.worldServerUrl;
        _subWorldConfig = SubWorldConfig.getSubWorldConfig(_params.subWorldConfigId);

        _worldConn = NetUtil.createWorldClient(_worldServerUrl);
        return true;
    }

    public static backToLogin() {
        tgxUIAlert.show("链接已断开，请重新登录").onClick(() => {
            tgxSceneUtil.loadScene(SceneDef.LOGIN);
        });
    }

    public static reset(){
        _worldConn = null;
    }

    public static async ensureConnected() {
        let ret = await this._connect();
        return ret;
    }
    private static async _connect(): Promise<{ isSucc: boolean, res?: ResJoinSubWorld, errMsg?: string }> {
        // Connect
        let resConnect = await WorldMgr.worldConn.connect();
        if (!resConnect.isSucc) {
            return { isSucc: false, errMsg: '连接到服务器失败: ' + resConnect.errMsg };
        }

        // JoinSubWorld
        let retJoin = await WorldMgr.worldConn.callApi('JoinSubWorld', {
            token: _params.token,
            uid: _params.uid,
            time: _params.time,
            subWorldId: _params.subWorldId,
        });
        if (!retJoin.isSucc) {
            return { isSucc: false, errMsg: '加入房间失败: ' + retJoin.err.message };
        }

        _currentUser = retJoin.res.currentUser;

        return { isSucc: true, res: retJoin.res };
    }

    public static get params(): SubWorldSceneParams {
        return _params;
    }
    public static get currentUser(): UserInfo {
        return _currentUser;
    }
    
    public static set playerNum(v: number) {
        _playerNum = v;
    }

    public static get playerNum() {
        return _playerNum;
    }

    public static get worldConn() {
        return _worldConn;
    }

    public static get worldServerUrl() {
        return _worldServerUrl;
    }

    public static set subWorldId(v: string) {
        _subWorldId = v;
    }

    public static get subWorldId(): string {
        return _subWorldId;
    }

    public static set subWorldConfig(v: SubWorldConfigItem) {
        _subWorldConfig = v;
    }

    public static get subWorldConfig() {
        return _subWorldConfig;
    }

    public static set subWorldDisplayName(v: string) {
        _subWorldDisplayName = v;
    }

    public static get subWorldDisplayName() {
        return this.subWorldDisplayName;
    }
}